package v1

import (
	bll "aggregation/bll/dashboard"
	model "aggregation/model/dashboard"
	"auth/utils"
	"strings"

	"github.com/gin-gonic/gin"
)

type market struct {
}

var Market = &market{}

func (m *market) Init(r *gin.RouterGroup) {
	router := r.Group("/dashboard/market") //, middleware.Auth())
	{
		router.GET("/board", m.marketDashBoard)                   // 营销头面板
		router.GET("/store/sale_rank", m.saleRank)                // 门店销售排行
		router.GET("/user_ride/wave", m.userRideWave)             // 用户用车用户图
		router.GET("/user_ride/last_month", m.userRideLastMonth)  // 用户上个月用车用户
		router.GET("/store/wave", m.storeIncreaseWave)            // 门店新增面板
		router.GET("/store_increase/board", m.storeIncreaseBoard) // 门店新增图
		router.GET("/sim/wave", m.simWave)                        // sim营收激活图
		router.GET("/sim/board", m.simBoard)                      // sim营收激活面板
		router.GET("nation/store_bike", m.nationStoreBike)        // 国内区域门店设备图
	}
}

func (m *market) marketDashBoard(ctx *gin.Context) {
	var (
		market model.MarketBoardResponse
		err    error
	)
	if market, err = bll.Market.MarketDashboard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, market)
}

func (m *market) saleRank(ctx *gin.Context) {
	var (
		rank model.StoreRankResponse
		err  error
	)
	if rank, err = bll.Market.SaleRank(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, rank)
}

func (m *market) userRideLastMonth(ctx *gin.Context) {
	var (
		ride model.RideLastMonthResponse
		err  error
	)
	if ride, err = bll.Market.UserRideLastmonth(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, ride)
}
func (m *market) userRideWave(ctx *gin.Context) {
	var (
		period string
		ride   model.RideWaveResponse
		err    error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))
	if ride, err = bll.Market.UserRideWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, ride)
}

func (m *market) storeIncreaseBoard(ctx *gin.Context) {
	var (
		increase model.StoreIncreaseBoardResponse
		err      error
	)
	if increase, err = bll.Market.StoreIncreaseBoard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, increase)
}

func (m *market) storeIncreaseWave(ctx *gin.Context) {
	var (
		period   string
		increase model.StoreIncreaseWaveResponse
		err      error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))

	if increase, err = bll.Market.StoreIncreaseWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, increase)
}

func (m *market) simBoard(ctx *gin.Context) {
	var (
		sim model.SimBoardResponse
		err error
	)
	if sim, err = bll.Market.SimBoard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, sim)
}

func (m *market) simWave(ctx *gin.Context) {
	var (
		period string
		sim    model.SimWaveResponse
		err    error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))
	if sim, err = bll.Market.SimWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, sim)
}
func (m *market) nationStoreBike(ctx *gin.Context) {
	var (
		nation model.NationStoreBikeResponse
		err    error
	)
	if nation, err = bll.Market.NationStoreBike(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, nation)
}
